package com.packagetracker.adapters;

import java.util.LinkedList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.packagetracker.R;
import com.packagetracker.carriers.PackageDetails;
import com.packagetracker.utils.DateUtil;
import com.packagetracker.views.ViewHolder;

public class PackageActivityAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private LinkedList<PackageDetails> details;
	private int resId;
	
	public PackageActivityAdapter(final Context context, final int resId, final LinkedList<PackageDetails> details) {
		this.inflater = LayoutInflater.from(context);
		this.resId = resId;
		this.details = details;
	}
	
	@Override
	public View getView(final int location, View convertView, final ViewGroup parent) { 
		ViewHolder holder;
		
		if (convertView == null) {
			convertView = this.inflater.inflate(this.resId, parent, false);
			
			holder = new ViewHolder();
			holder.setDate((TextView)convertView.findViewById(R.id.info_item_date));
			holder.setTime((TextView)convertView.findViewById(R.id.info_item_time));
			holder.setDescription((TextView)convertView.findViewById(R.id.info_item_description));
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder)convertView.getTag();
		}
		
		final PackageDetails details = this.details.get(location);
		
		holder.getDate().setText(DateUtil.toDate("yyyyMMdd", "MM/dd/yyyy", details.getDate()));
		holder.getTime().setText(DateUtil.toDate("hhmmss", "hh:mm a", details.getTime()));
		holder.getDescription().setText(details.getDescription());
		
		return convertView;
	}
	
	@Override
	public long getItemId(final int location) {  
		return location;
	}
	
	@Override
	public Object getItem(final int location) {     
		return this.details.get(location);
	}
	
	@Override
	public int getCount() {                        
		return this.details.size();
	}
	
}